package com.google.code.irouter.portal.client.dashboard.main;

import com.google.code.irouter.portal.client.dashboard.main.center.Managers;
import com.google.code.irouter.portal.client.dashboard.main.north.menu.switcher.Solutions;
import com.google.code.irouter.portal.client.rpc.solution.SolutionResult;
import com.google.code.irouter.portal.client.rpc.solution.SolutionService;
import com.google.code.irouter.portal.client.rpc.solution.SolutionServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.Ext;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.layout.FitLayout;

public final class Center extends Panel {
	
	private final SolutionServiceAsync solutionService = GWT.create(SolutionService.class);

	public Center() {
		super();
		this.setId(Center.class.getName());

		this.setBorder(false);
		this.setLayout(new FitLayout()); 
		this.add(new Managers());
		this.addListener(new CenterListener());
		this.show();
	}
	
	public void init() {
		((Managers) Ext.getCmp(Managers.class.getName())).mask();
		solutionService.get(
				new AsyncCallback<SolutionResult>() {
					public void onFailure(Throwable caught) {
					// TODO
					}

					public void onSuccess(SolutionResult result) {
						if(result != null) {
							Cookies.setCookie(Solutions.COOKIE_SOLUTION, result.getId());
							((Managers) Ext.getCmp(Managers.class.getName())).unmask();
						}						
					}
				}
		);
	}
}
